package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.dto.BookkeepingDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.BookkeepingDetailsMapper;
import com.ant.storage.mapper.BookkeepingMapper;
import com.ant.storage.pojo.Bookkeeping;
import com.ant.storage.pojo.BookkeepingDetails;
import com.ant.storage.service.IBookkeepingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzd
 * @since 2024-06-13
 */
@Service
public class BookkeepingServiceImpl extends ServiceImpl<BookkeepingMapper, Bookkeeping> implements IBookkeepingService {

 
    @Resource
    private BookkeepingDetailsMapper detailsMapper;

    @Override
    public PageInfo<Bookkeeping> queryList(BookkeepingDto dto) {
        try {
            PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
            QueryWrapper<Bookkeeping> wrapper = new QueryWrapper<>();
            wrapper.orderByAsc("id");

            List<Bookkeeping> list = this.baseMapper.selectList(wrapper);
            list.stream().filter(item -> {
                List<BookkeepingDetails> details = detailsMapper.selectList(new QueryWrapper<BookkeepingDetails>()
                        .in("id", item.getBookkeepingId().split(",")));
                item.setDetails(details);
                return true;
            }).collect(Collectors.toList());
            PageInfo<Bookkeeping> info = new PageInfo<>(list);
            return info;
        } catch (Exception e) {
            throw new AntException(ResultCode.BODY_NOT_MATCH.getResultCode(),e.getMessage());
        }
    }

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3);
        System.out.println(list);
    }
}
